#!/bin/sh

# At the end of the document, you will find a script that can help
# to create a cross-compiler on Debian. However, currently (11/2011),
# Debian is migrating toward multiarch and the script does not work.
#
# What I propose is to install Debian packages of a GCC MIPS cross-compiler
# that has been compiled from and on a Debian squeeze (stable)
# These packages should work on Debian stable (squeeze), testing (wheesly)
# and unstable (sid). They should probably also work on Ubuntu (not tested,
# please report feedback).
#
# Only the AMD64 architecture is supported by these pre-compiled packages, not
# the i386 one.
#
# Just run this script (or look at it and type the commands yourself) to
# install a MIPS cross-compiler

set -e

# either this script must be called as root or the user must be
# able to use sudo
if [ "$(whoami)" = root ]; then
	ROOT=
else
	ROOT=sudo
fi

# Check that we are on amd64 or i386
case "$(uname -m)" in
x86_64)
	ARCH=amd64
	;;
i*86)
	ARCH=i386
	;;
*)
	echo "Wrong architechture $(uname -m). Aborting." 1>&2
	exit 1
esac

# install a new sources of Debian package
if [ -f /etc/apt/sources.list.d/nachos.list ]; then
	$ROOT sed -e 's/ nachos/ mipsel-cross/' -i /etc/apt/sources.list.d/nachos.list
	echo "File /etc/apt/sources.list.d/nachos.list already installed"
else
	echo "deb http://moais.imag.fr/membres/vincent.danjean/debian sid mipsel-cross" | \
		$ROOT bash -c "cat > /etc/apt/sources.list.d/nachos.list"
fi
# install the gpg key of the archive
echo "Installing the key of the archive"
echo "Check it if you are concerned by security (you should)"
wget -O - http://moais.imag.fr/membres/vincent.danjean/pubkey.asc | \
	$ROOT apt-key add -

# update the list of available packages
$ROOT apt-get update

# install the cross-compiler
$ROOT apt-get install gcc-4.4-mipsel-linux-gnu

if dpkg -l g++-multilib | grep -s '^ii' ; then
	:
else
	if test "$ARCH" = amd64 ; then
		echo "**************************************************************"
		echo "* Installing the g++-multilib package from your distrib      *"
		echo "* in order to compile NachOS                                 *"
		echo "**************************************************************"
		echo "* Refuse the installation (and try to install it yourself    *"
		echo "* with your usual package manager) if something seems wrong  *"
		echo "* (packages that need to be removed, to many packages        *"
		echo "* upgraded, ...)                                             *"
		echo "**************************************************************"
		$ROOT apt-get install g++-multilib
	fi
fi

echo "Ok, you should be able to compile NachOS"

# never go further: creating a MIPS cross-compiler is not working for now
exit 0

# Here is a script to build and install a cross compiler to be used on Nachos
#
# Contrary to previous document, this script build a recent cross compiler
# (gcc 4.6 by default) so gcc 2.95 is not used anymore.
# improvement: software is installed as real packages on the system
# inconvenient: only works for Debian (unstable and perhaps
#   testing/wheezy) and it could perhaps work with ubuntu (feedback welcome)
#
# Note: recent gcc requires the target libc to build the cross-compiler.
# This is why I cannot tell how to build one only from gcc sources
# (as it was done with gcc 2.95)

# I think I put all required commands. Howver, it is probably better
# to copy and paste the command to be able to look at the results
#
# WARNING: building the cross compiler itself requires lots of time and
# lots of available disk space. It is probably possible to reduce this
# if we build only the C cross compiler (and not C++, ...) but I did not
# look if this can be easily done.

# Do not copy-paste the next line in a interractive shell
# (else your shell will exit as soon as a command does not succeed)
set -ex

mkdir -p tools
cd tools

TARGET=mipsel-linux-gnu
if test "$(ls binutils-$TARGET_*.deb 2>/dev/null)" ; then
	echo "Skipping building and installing binutils (already there)"
else
	apt-get source binutils
	sudo apt-get build-dep binutils
	sudo apt-get install fakeroot build-essential binutils-multiarch
	cd binutils-*
	# on squeeze, replace the following line by
        # env TARGET=$TARGET fakeroot debian/rules binary-cross
	env TARGET=$TARGET dpkg-buildpackage -b -uc -us
	cd ..
	sudo dpkg -i binutils-$TARGET_*.deb
fi

sudo apt-get install dpkg-cross xapt
sudo xapt --mirror http://ftp.fr.debian.org/debian/ \
  --arch mipsel --suite unstable --keep-cache libc6-dev libc6 \
  libc6-dev-mips64 libc6-dev-mipsn32 libc6-mips64 linux-libc-dev libc6-mipsn32

# At least 4.5. Previous does not have a correct cross-compiler build setup
GCC_V=4.6

apt-get source gcc-${GCC_V}
sudo apt-get build-dep gcc-${GCC_V}
sudo apt-get install gcc-${GCC_V}-source

cd gcc-${GCC_V}-*
env GCC_TARGET=mipsel debian/rules control
env GCC_TARGET=mipsel dpkg-buildpackage -rfakeroot -us -uc -b

cd ..
sudo dpkg -i gcc-${GCC_V}-mipsel-linux-gnu_*.deb gcc-${GCC_V}-mipsel-linux-gnu-base_*.deb  cpp-${GCC_V}-mipsel-linux-gnu_*.deb  libgcc1-mipsel-cross_*.deb  libgomp1-mipsel-cross_*.deb

# Once the packages are installed, all the tools directory can be removed.

# This version of nachos will automatically detect and use this cross-compiler
# Enjoy.
